package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.data.GroupPayData;
import com.banzhi.shop.data.PartnerCheckstandData;
import com.banzhi.shop.model.CallBack.GroupPayCallback;
import com.banzhi.shop.model.IGroupPayModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.security.acl.Group;

/**
 * Created by Administrator on 2018/7/24.
 */
//拼团支付接口 /*支付方式 24微信支付 22支付宝 支付 1余额支付*/
public class GroupPayImpl implements IGroupPayModel{
    @Override
    public void PayYure(String orderno, String paytype, final GroupPayCallback callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.GroupPayURL);
        if (MyApp.loginMessage.getString("flag", "").equals("3")) {
         //   LogUtils.e("mTT" ,MyApp.loginMessage.getString("mTt", ""));
            params.addBodyParameter("tt",1+"");
          //  params.addBodyParameter("teamid",y);
        }
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("paytype", "credit");
        params.addBodyParameter("orderno", orderno);
        LogUtils.e("GroupPayImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        GroupPayData groupPayData = gson.fromJson(result, GroupPayData.class);
                        callBack.onPayYueGroupPaySuccess(groupPayData.getData());
                    } else {
                        callBack.onPayWXGroupPayError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PayYuE1111", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });

    }
    @Override
    public void PayWX(String orderno, String paytype, final GroupPayCallback callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.GroupPayURL);
        if (!MyApp.loginMessage.getString("teamid", "").isEmpty()) {
            params.addBodyParameter("tt",1+"");
        }
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("paytype", "wechat");
        params.addBodyParameter("orderno", orderno);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        GroupPayData groupPayData = gson.fromJson(result, GroupPayData.class);
                        callBack.onPayYueGroupPaySuccess(groupPayData.getData());
                    } else {
                        callBack.onPayWXGroupPayError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PayYuE1111", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });

    }
    @Override
    public void PayZFB(String orderno, String paytype, final GroupPayCallback callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.GroupPayURL);
        if (!MyApp.loginMessage.getString("teamid", "").isEmpty()) {
            params.addBodyParameter("tt",1+"");
        }
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("paytype", "alipay");
        params.addBodyParameter("orderno", orderno);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        GroupPayData groupPayData = gson.fromJson(result, GroupPayData.class);
                        callBack.onPayYueGroupPaySuccess(groupPayData.getData());
                    } else {
                        callBack.onPayWXGroupPayError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PayYuE1111", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

}
